home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 January / macformat-020.iso / Shareware City / Developers / OutOfPhase1.01Source / OutOfPhase Folder / Level 0 Macintosh 07Aug94 / StartupOpen.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-01  |  9.3 KB  |  288 lines  |  [TEXT/KAHL]

  1. /* StartupOpen.c */
  2. /*****************************************************************************/
  3. /*                                                                           */
  4. /*    System Dependency Library for Building Portable Software               */
  5. /*    Macintosh Version                                                      */
  6. /*    Written by Thomas R. Lawrence, 1993 - 1994.                            */
  7. /*                                                                           */
  8. /*    This file is Public Domain; it may be used for any purpose whatsoever  */
  9. /*    without restriction.                                                   */
  10. /*                                                                           */
  11. /*    This package is distributed in the hope that it will be useful,        */
  12. /*    but WITHOUT ANY WARRANTY; without even the implied warranty of         */
  13. /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                   */
  14. /*                                                                           */
  15. /*    Thomas R. Lawrence can be reached at tomlaw@world.std.com.             */
  16. /*                                                                           */
  17. /*****************************************************************************/
  18.  
  19. #include "MiscInfo.h"
  20. #include "Debug.h"
  21. #include "Audit.h"
  22. #include "Definitions.h"
  23.  
  24. #pragma options(pack_enums)
  25. #include <AppleEvents.h>
  26. #pragma options(!pack_enums)
  27.  
  28. #include "StartupOpen.h"
  29. #include "Memory.h"
  30. #include "Array.h"
  31. #include "Files.h"
  32.  
  33.  
  34. /* this contains a list of things that have been received for opening.*/
  35. /* if StartupList is NIL, then the open event hasn't been received.  Otherwise, */
  36. /* it contains FileSpecs to be opened */
  37. static ArrayRec*                    StartupList = NIL;
  38.  
  39. /* debugging flag */
  40. EXECUTE(static MyBoolean    Initialized = False;)
  41.  
  42. /* flag that gets set True when a quit signal is received */
  43. static MyBoolean                    QuitPending = False;
  44.  
  45.  
  46. /* function to check to see that all required parameters have been gotten */
  47. static OSErr        MyGotRequiredParams(AppleEvent* theAppleEvent)
  48.     {
  49.         DescType        ReturnedType;
  50.         Size                ActualSize;
  51.         OSErr                Error;
  52.  
  53.         Error = AEGetAttributePtr(theAppleEvent, keyMissedKeywordAttr, typeWildCard,
  54.             &ReturnedType,NIL,0,&ActualSize);
  55.         if (Error == errAEDescNotFound)
  56.             {
  57.                 return noErr;  /* we got all the params, since no more were found */
  58.             }
  59.          else
  60.             {
  61.                 if (Error == noErr)
  62.                     {
  63.                         return errAEEventNotHandled;  /* missed some, so it failed */
  64.                     }
  65.                  else
  66.                     {
  67.                         return Error;  /* AEGetAttributePtr failed, so we return why */
  68.                     }
  69.             }
  70.     }
  71.  
  72.  
  73. /* handler for open application--presents an untitled document */
  74. static pascal    OSErr        MyHandleOApp(AppleEvent* theAppleEvent,
  75.                                                 AppleEvent* reply, long handlerRefcon)
  76.     {
  77.         OSErr        Error;
  78.  
  79.         APRINT(("+MyHandleOApp"));
  80.         Error = MyGotRequiredParams(theAppleEvent);
  81.         ERROR(Error!=noErr,PRERR(AllowResume,"MyHandleOApp error."));
  82.         if (Error != noErr)
  83.             {
  84.                 APRINT(("-MyHandleOApp Error %s",Error));
  85.                 return Error;
  86.             }
  87.         StartupList = NewArray();
  88.         APRINT(("-MyHandleOApp"));
  89.         return noErr;
  90.     }
  91.  
  92.  
  93. /* handler for open documents */
  94. static pascal    OSErr        MyHandleODoc(AppleEvent* theAppleEvent,
  95.                                                 AppleEvent* reply, long handlerRefcon)
  96.     {
  97.         OSErr                        Error;
  98.         long                        Index,ItemsInList;
  99.         AEDescList            DocList;
  100.         Size                        ActualSize;
  101.         AEKeyword                Keywd;
  102.         DescType                ReturnedType;
  103.  
  104.         APRINT(("+MyHandleODoc"));
  105.         /* get the direct parameter--a descriptor list--and put it into DocList */
  106.         Error = AEGetParamDesc(theAppleEvent, keyDirectObject, typeAEList, &DocList);
  107.         ERROR(Error!=noErr,PRERR(AllowResume,"MyHandleODoc error."));
  108.         if (Error != noErr) return Error;
  109.         /* check for missing required parameters */
  110.         Error = MyGotRequiredParams(theAppleEvent);
  111.         if (Error != noErr) return Error;
  112.         ERROR(Error!=noErr,PRERR(AllowResume,"MyHandleODoc error."));
  113.         /* count the number of descriptor records in the list */
  114.         Error = AECountItems(&DocList,&ItemsInList);
  115.         ERROR(Error!=noErr,PRERR(AllowResume,"MyHandleODoc error."));
  116.         /* now get each descriptor record from the list, coerce the returned data */
  117.         /* to an FSSpec record, and open the associated file */
  118.         if (StartupList == NIL)
  119.             {
  120.                 StartupList = NewArray();
  121.             }
  122.         if (StartupList == NIL)
  123.             {
  124.                 /* error -- not enough memory to create the list */
  125.                 goto OutOfMemPoint;
  126.             }
  127.         for (Index=1; Index <= ItemsInList; Index += 1)
  128.             {
  129.                 FSSpec                    MyFSS;
  130.  
  131.                 Error = AEGetNthPtr(&DocList,Index,typeFSS,&Keywd,&ReturnedType,
  132.                     (void*)&MyFSS,sizeof(FSSpec),&ActualSize);
  133.                 ERROR(Error!=noErr,PRERR(AllowResume,"MyHandleODoc error."));
  134.                 if (Error == noErr)
  135.                     {
  136.                         FSSpec*                    ArrayElement;
  137.                         long                        CharIndex;
  138.  
  139.                         ArrayElement = (FSSpec*)AllocPtrCanFail(sizeof(FSSpec),"StartupOpenFSSpec");
  140.                         if (ArrayElement == NIL)
  141.                             {
  142.                              FailurePoint1:
  143.                                 goto OutOfMemPoint;
  144.                             }
  145.                         *ArrayElement = MyFSS;
  146.                         if (!ArrayAppendElement(StartupList,ArrayElement))
  147.                             {
  148.                              FailurePoint2:
  149.                                 ReleasePtr((char*)ArrayElement);
  150.                                 goto FailurePoint1;
  151.                             }
  152.                         if (!Eep_RegisterFileSpec((FileSpec*)ArrayElement))
  153.                             {
  154.                                 ArrayDeleteElement(StartupList,
  155.                                     ArrayFindElement(StartupList,ArrayElement));
  156.                                 goto FailurePoint2;
  157.                             }
  158.                     }
  159.             }
  160.      OutOfMemPoint:
  161.         Error = AEDisposeDesc(&DocList);
  162.         ERROR(Error!=noErr,PRERR(AllowResume,"MyHandleODoc error."));
  163.         APRINT(("-MyHandleODoc"));
  164.         return Error;
  165.     }
  166.  
  167.  
  168. /* handle a quit event */
  169. static pascal    OSErr        MyHandleQuit(AppleEvent* theAppleEvent,
  170.                                                 AppleEvent* reply, long handlerRefcon)
  171.     {
  172.         OSErr            Error;
  173.  
  174.         APRINT(("+MyHandleQuit"));
  175.         /* check for missing required parameters */
  176.         Error = MyGotRequiredParams(theAppleEvent);
  177.         ERROR(Error!=noErr,PRERR(AllowResume,"CApplication::MyHandleQuit error."));
  178.         if (Error != noErr) return Error;
  179.         QuitPending = True;
  180.         APRINT(("-MyHandleQuit"));
  181.         return noErr;
  182.     }
  183.  
  184.  
  185. /* compile a list of files that should be opened when the program starts up. */
  186. /* The parameters should be exactly the ones passed into main() upon startup. */
  187. /* It is implementation defined as to whether they will be used; the Macintosh */
  188. /* does not use them, but uses Apple Events for opening startup documents instead. */
  189. void                    PrepareStartupDocuments(int argc, char* argv[])
  190.     {
  191.         APRINT(("+PrepareStartupDocuments"));
  192.         ERROR(Initialized,PRERR(ForceAbort,
  193.             "PrepareStartupDocuments called multiple times"));
  194.         EXECUTE(Initialized = True;)
  195.         StartupList = NIL;
  196.         AEInstallEventHandler(kCoreEventClass,kAEOpenApplication,
  197.             (EventHandlerProcPtr)&MyHandleOApp,0,False);
  198.         AEInstallEventHandler(kCoreEventClass,kAEOpenDocuments,
  199.             (EventHandlerProcPtr)&MyHandleODoc,0,False);
  200.         AEInstallEventHandler(kCoreEventClass,kAEQuitApplication,
  201.             (EventHandlerProcPtr)&MyHandleQuit,0,False);
  202.         APRINT(("-PrepareStartupDocuments"));
  203.     }
  204.  
  205.  
  206. /* Get a startup item.  It will initially return False.  Once the open event is */
  207. /* received, it will return True from then on.  If there is a file specification */
  208. /* to get, it will be returned, otherwise NIL will be returned.  The file */
  209. /* specification should be disposed of with DisposeFileSpec.  Here's how you know if */
  210. /* you should open an untitled document:  the first time it returns True, if it */
  211. /* also returns NIL, then do it. */
  212. MyBoolean            GetStartupObject(struct FileSpec** ReturnStuff)
  213.     {
  214.         ERROR(!Initialized,PRERR(ForceAbort,"GetStartupList:  StartupOpen not initialized"));
  215.         if (StartupList == NIL)
  216.             {
  217.                 return False;
  218.             }
  219.          else
  220.             {
  221.                 if (ArrayGetLength(StartupList) != 0)
  222.                     {
  223.                         *ReturnStuff = (FileSpec*)ArrayGetElement(StartupList,0);
  224.                         ArrayDeleteElement(StartupList,0);
  225.                     }
  226.                  else
  227.                     {
  228.                         *ReturnStuff = NIL;
  229.                     }
  230.                 return True;
  231.             }
  232.     }
  233.  
  234.  
  235. /* clean up any internal structures allocated by PrepareStartupDocument. */
  236. void                    ClearStartupDocuments(void)
  237.     {
  238.         APRINT(("+ClearStartupDocuments"));
  239.         ERROR(!Initialized,PRERR(ForceAbort,"GetStartupList:  StartupOpen not initialized"));
  240.         if (StartupList != NIL)
  241.             {
  242.                 while (ArrayGetLength(StartupList) != 0)
  243.                     {
  244.                         ReleasePtr((char*)ArrayGetElement(StartupList,0));
  245.                         ArrayDeleteElement(StartupList,0);
  246.                     }
  247.                 DisposeArray(StartupList);
  248.                 StartupList = NIL;
  249.             }
  250.         AERemoveEventHandler(kCoreEventClass,kAEOpenApplication,
  251.             (EventHandlerProcPtr)&MyHandleOApp,False);
  252.         AERemoveEventHandler(kCoreEventClass,kAEOpenDocuments,
  253.             (EventHandlerProcPtr)&MyHandleODoc,False);
  254.         AERemoveEventHandler(kCoreEventClass,kAEQuitApplication,
  255.             (EventHandlerProcPtr)&MyHandleQuit,False);
  256.         APRINT(("-ClearStartupDocuments"));
  257.     }
  258.  
  259.  
  260. /* this returns True if the system would like the program to quit.  The program */
  261. /* should then ask the user if he wants to save all changed documents. */
  262. /* A Quit event on the Macintosh will cause this to return True */
  263. MyBoolean            CheckQuitPending(void)
  264.     {
  265.         return QuitPending;
  266.     }
  267.  
  268.  
  269. /* If some other signal besides an implementation defined system quit signal */
  270. /* is received, this can be used to indicate such, and cause a normal shutdown */
  271. /* of the program to occur. */
  272. void                    SetQuitPending(void)
  273.     {
  274.         APRINT(("+SetQuitPending"));
  275.         QuitPending = True;
  276.         APRINT(("-SetQuitPending"));
  277.     }
  278.  
  279.  
  280. /* If the user cancels the quit, this should be used to clear the flag and */
  281. /* allow the program to continue running */
  282. void                    AbortQuitInProgress(void)
  283.     {
  284.         APRINT(("+AbortQuitInProgress"));
  285.         QuitPending = False;
  286.         APRINT(("-AbortQuitInProgress"));
  287.     }
  288.